package com.ruoyi.backend.controller.app;

import com.ruoyi.backend.domain.Order;
import com.ruoyi.backend.domain.dto.OrderDto;
import com.ruoyi.backend.enums.RoleKeyEnum;
import com.ruoyi.backend.service.IOrderService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单Controller
 * 
 * @author wjr
 * @date 2025-09-23
 */
@RestController
@RequestMapping("/app/backend/t_order")
@Api(tags = "app-订单")
public class AppOrderController extends BaseController
{
    @Autowired
    private IOrderService orderService;

    /**
     * 查询订单列表
     */
//    @PreAuthorize("@ss.hasPermi('backend:t_order:list')")
    @GetMapping("/list")
    @ApiOperation("查询订单列表")
    public TableDataInfo list(OrderDto orderDto)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        //根据查询方式不同，设置不同的查询参数
        if (RoleKeyEnum.CUSTOMER.getCode().equals(orderDto.getSelectUserRoleKey())) {
            //此时登录账号以客户的身份查询订单列表
            orderDto.setCustomerId(user.getUserId().intValue());
        } else if (RoleKeyEnum.STORE_MANAGER.getCode().equals(orderDto.getSelectUserRoleKey())) {
            //此时登录账号以店铺管理员的身份查询订单列表
            orderDto.setShopId(user.getShopId());
        } else if (RoleKeyEnum.STORE_EMPLOYEE.getCode().equals(orderDto.getSelectUserRoleKey())) {
            //此时登录账号以店铺员工的身份查询订单列表
            orderDto.setChargeId(user.getUserId().intValue());
        }

        //判断如果是客户，将客户id放入查询条件中
//        SysUser user = SecurityUtils.getLoginUser().getUser();
//        if (user != null && user.getRoles() != null) {
//            List<String> roleKeys = user.getRoles().stream().map(SysRole::getRoleKey).collect(Collectors.toList());
//            if (roleKeys.contains(RoleKeyEnum.CUSTOMER.getCode())) {
//                //此时登录用户为客户
//                orderDto.setCustomerId(user.getUserId().intValue());
//            } else if (roleKeys.contains(RoleKeyEnum.STORE_EMPLOYEE.getCode())) {
//                //此时登录用户为门店员工，只能看自己负责的订单
//                orderDto.setChargeId(user.getUserId().intValue());
//            } else if (roleKeys.contains(RoleKeyEnum.STORE_MANAGER.getCode())) {
//                //此时登录用户为门店管理员，可以看自己门店的订单
//                orderDto.setShopId(user.getShopId());
//            }
//        }

        startPage();
        List<Order> list = orderService.selectOrderList(orderDto);
        return getDataTable(list);
    }

    /**
     * 获取订单详细信息
     */
//    @PreAuthorize("@ss.hasPermi('backend:t_order:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取订单详细信息")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(orderService.selectOrderById(id));
    }

    /**
     * 新增订单
     */
//    @PreAuthorize("@ss.hasPermi('backend:t_order:add')")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增订单")
    public AjaxResult add(@RequestBody Order order)
    {
        return toAjax(orderService.insertOrder(order));
    }

    /**
     * 修改订单
     */
//    @PreAuthorize("@ss.hasPermi('backend:t_order:edit')")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改订单")
    public AjaxResult edit(@RequestBody Order order)
    {
        return toAjax(orderService.updateOrder(order));
    }

    /**
     * 删除订单
     */
//    @PreAuthorize("@ss.hasPermi('backend:t_order:remove')")
    @Log(title = "订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除订单")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(orderService.deleteOrderByIds(ids));
    }

    /**
     * 订单分配负责人，确认，结算，评价等
     */
//    @PreAuthorize("@ss.hasPermi('backend:t_order:edit')")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping("/changeOrder")
    @ApiOperation("订单分配负责人，确认，结算，评价等")
    public AjaxResult changeOrder(@RequestBody OrderDto orderDto)
    {
        return toAjax(orderService.changeOrder(orderDto));
    }

    /**
     * 导出订单列表
     */
    /*@PreAuthorize("@ss.hasPermi('backend:t_order:export')")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Order order)
    {
        List<Order> list = orderService.selectOrderList(order);
        ExcelUtil<Order> util = new ExcelUtil<Order>(Order.class);
        util.exportExcel(response, list, "订单数据");
    }*/

}
